﻿namespace GeneralTool.CoreLibrary.Extensions
{
    /// <summary>
    /// 反射帮助类
    /// </summary>
    public static class RefexExtensions
    {
        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="method"></param>
        /// <param name="datas"></param>
        /// <returns></returns>
        public static object InvokeMethod(this object obj, string method, params object[] datas)
        {
            return obj.GetType().GetMethod(method).Invoke(obj, datas);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="method"></param>
        /// <param name="datas"></param>
        /// <returns></returns>
        public static T InvokeMethod<T>(this object obj, string method, params object[] datas)
            => (T)InvokeMethod(obj, method, datas);

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static object GetValue(this object obj, string propertyName, params object[] indexs)
        {
            return obj.GetType().GetProperty(propertyName).GetValue(obj, indexs);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static T GetValue<T>(this object obj, string propertyName, params object[] indexs)
            => (T)GetValue(obj, propertyName);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        /// <param name="indexs"></param>
        public static void SetValue(this object obj, string propertyName, object value, params object[] indexs)
        {
            obj.GetType().GetProperty(propertyName).SetValue(obj, value, indexs);
        }

        
    }
}
